package com.lz.gmall.order.listener;

import com.lz.gmall.order.service.OrderInfoService;
import com.lz.gmall.rabbit.constant.RabbitConstant;
import com.lz.gmall.rabbit.service.RabbitBizService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author 郭运龙
 */
@Component
public class WareStockListener {

    @Autowired
    private RabbitBizService rabbitBizService;

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 监听从库存系统发送的消息，进行修改订单的状态
     */
    @RabbitListener(queues = RabbitConstant.QUEUE_WARE_ORDER)
    public void wareStock(String msg, Message message, Channel channel){
        try {

            orderInfoService.updateOrderStatusAfterWare(msg);

            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
        } catch (Exception e) {
            e.printStackTrace();
            rabbitBizService.retryer(msg,message,channel,3L);
        }
    }
}
